from bs4 import BeautifulSoup
from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from mysql.connector import Error

from utils import MysqlConnectInstance, WebDriverInstance, ReUtils


# 1. 定时任务-获取最近10条数据
def get_data_after_click(url, button_selector, keyword):
    driver = WebDriverInstance().get_driver()
    # 打开指定的URL
    driver.get(url)

    try:
        # 等待按钮出现并点击按钮
        button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, button_selector))
        )
        # 获取页面内容
        page_source = driver.page_source

        # 使用BeautifulSoup解析页面内容
        soup = BeautifulSoup(page_source, 'html.parser')
        links = soup.find_all('li')
        # 提取包含关键词的链接和标题
        filtered_links = [(link.get('data-link'), link.get('data-title')) for link in links if
                          link.get('data-link') and keyword in link.get('data-title')]
        return filtered_links

    except TimeoutException:
        print("等待超时。检查定位器或增加等待时间。")
        driver.quit()

    # finally:
        # 关闭浏览器
        # driver.quit()
    print('get data list done!')


def write_to_database(data_after_click):
    connection = MysqlConnectInstance().get_connection()
    # 如果 数据库存在，则不操作
    cursor = connection.cursor()
    check_query = "SELECT * FROM mp_url_log WHERE rate_date = %s"
    insert_query = "INSERT INTO mp_url_log (rate_date,url, has_grab) VALUES (%s,%s,0)"

    try:
        for href, title in data_after_click:
            rate_date = ReUtils.extract_date(title)

            cursor.execute(check_query, (rate_date,))
            result = cursor.fetchone()

            if result:
                print(f"URL 已存在: {rate_date}")
            else:
                cursor.execute(insert_query, (rate_date, href))
                connection.commit()
                print(f"URL 插入成功: {href}")
    except Error as e:
        print(f"Error: '{e}'")
    print('all done')
    pass


def main():
    # 示例URL和按钮选择器
    url = 'https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg5NDc0NTIwNA==&action=getalbum&album_id=3406101716922482691&scene=173&subscene=&sessionid=svr_66b515cb9d5&enterid=1716018373&from_msgid=2247490570&from_itemidx=1&count=3&nolastread=1&is_reverse=1#wechat_redirect'
    button_selector = 'div.album-sort__wrp'  # 替换为按钮的CSS选择器
    keyword = '汇率参考'
    data_after_click = get_data_after_click(url, button_selector, keyword)
    write_to_database(data_after_click)
    print('获取地址完成...')

if __name__ == '__main__':
    main()